package cn.topcodes.tcsf.wechat.service;

import cn.topcode.unicorn.utils.StringUtil;
import cn.topcode.unicorn.wxsdk.WXContext;
import cn.topcode.unicorn.wxsdk.base.Result;
import cn.topcode.unicorn.wxsdk.base.WXResponseCode;
import cn.topcode.unicorn.wxsdk.message.kf.*;
import cn.topcode.unicorn.wxsdk.message.receive.msg.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Unicorn on 2017/8/17.
 */
@Service
public class WechatKfMessageServiceImpl implements WechatKfMessageService {

    @Autowired
    private WechatMpService wechatMpService;

    /**
     * 发送客服文本消息
     * @param openId
     * @param text
     * @param kfAccount
     */
    @Override
    public void sendText(String mpId, String openId, String text, String kfAccount) {
        ReqSendKfMessage msg = new ReqSendKfMessage();
        msg.setToUser(openId);
        msg.setMsgType(Message.MSG_TYPE_TEXT);
        KfTextMessage textMsg = new KfTextMessage();
        textMsg.setContent(text);
        msg.setText(textMsg);
        sendKfMessage(mpId, msg, kfAccount);
    }

    @Override
    public void sendText(String openId, String text, String kfAccount) {
        this.sendText(wechatMpService.getDefaultMpId(), openId, text, kfAccount);
    }

    /**
     * 发送客服图片消息
     * @param openId
     * @param mediaId
     * @param kfAccount
     */
    @Override
    public void sendImage(String mpId, String openId, String mediaId, String kfAccount) {
        sendVoiceOrImage(mpId, Message.MSG_TYPE_IMAGE, openId, mediaId, kfAccount);
    }

    @Override
    public void sendImage(String openId, String mediaId, String kfAccount) {
        this.sendImage(wechatMpService.getDefaultMpId(), openId, mediaId, kfAccount);
    }

    /**
     * 发送客服语音消息
     * @param openId
     * @param mediaId
     * @param kfAccount
     */
    @Override
    public void sendVoice(String mpId, String openId, String mediaId, String kfAccount) {
        sendVoiceOrImage(mpId, Message.MSG_TYPE_VOICE, openId, mediaId, kfAccount);
    }

    @Override
    public void sendVoice(String openId, String mediaId, String kfAccount) {
        this.sendVoice(wechatMpService.getDefaultMpId(), openId, mediaId, kfAccount);
    }

    /**
     * 发送客服视频消息
     * @param openId
     * @param mediaId
     * @param thumbMediaId
     * @param title
     * @param description
     * @param kfAccount
     */
    @Override
    public void sendVideo(String mpId, String openId, String mediaId, String thumbMediaId, String title, String description, String kfAccount) {
        ReqSendKfMessage msg = new ReqSendKfMessage();
        msg.setToUser(openId);
        msg.setMsgType(Message.MSG_TYPE_VIDEO);
        KfVideoMessage videoMessage = new KfVideoMessage();
        videoMessage.setMediaId(mediaId);
        videoMessage.setThumbMediaId(thumbMediaId);
        videoMessage.setThumbMediaId(title);
        videoMessage.setDescription(description);
        msg.setVideo(videoMessage);
        sendKfMessage(mpId, msg, kfAccount);
    }

    @Override
    public void sendVideo(String openId, String mediaId, String thumbMediaId, String title, String description, String kfAccount) {
        this.sendVideo(wechatMpService.getDefaultMpId(), openId, mediaId, thumbMediaId, title, description, kfAccount);
    }

    /*发送图片或语音*/
    private void sendVoiceOrImage(String mpId, String type, String openId, String mediaId, String kfAccount) {
        ReqSendKfMessage msg = new ReqSendKfMessage();
        msg.setToUser(openId);
        msg.setMsgType(type);
        KfImageOrVoiceMessage imageMessage = new KfImageOrVoiceMessage();
        imageMessage.setMediaId(mediaId);
        if(Message.MSG_TYPE_IMAGE.equals(type)) {
            msg.setImage(imageMessage);
        }else {
            msg.setVoice(imageMessage);
        }
        sendKfMessage(mpId, msg, kfAccount);
    }

    /*发送客服消息*/
    private void sendKfMessage(String mpId, ReqSendKfMessage msg, String kfAccount) {
        if(StringUtil.isNotBlank(kfAccount)) {
            CustomService customService = new CustomService();
            customService.setKfAccount(kfAccount);
            msg.setCustomService(customService);
        }
        Result result = WXContext.getMessageInvoker().sendKfMessage(mpId, msg);
        if(result == null || result.getErrcode() == null || result.getErrcode() != WXResponseCode.CODE_0_SUCCESS) {
            throw new IllegalArgumentException(result.getErrcode() + ":" + result.getErrmsg());
        }
    }

}
